<template>
    <div class="table_box">
        <TableCard title="救援车辆管理" :table_data="table_data" :table_item="table_item" :user="'car'" @SizeChange="SizeChange"
            @currentPage="currentPage" @is_add_dialog_visible="is_add_dialog_visible" @row_id="row_id" @add="add"
            :total="total" :loading="loading" />
        <el-dialog title="救援车辆信息维护" :visible.sync="add_dialog_visible" center width="40%">
            <el-form :model="add_form" label-position="right" :rules="rules" ref="add_form_ref">
                <el-form-item label="车牌号">
                    <el-input v-model="add_form.license_number" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="驾驶员" size="large">
                    <el-input v-model="add_form.driver_name" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="所在企业" size="large">
                    <el-input v-model="add_form.enterprise_name" autocomplete="off"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="add_dialog_visible = false" size="medium">取 消</el-button>
                <el-button type="primary" @click="submit" size="medium">确 定</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import TableCard from '@/components/reusable_components/table_card.vue'
import { rules } from '@/utils/rules.js'
export default {
    components: {
        TableCard,
    },

    data() {
        return {
            table_item: [
                // { key: 'index', value: '序号', default: '' },
                // { key: 'type', value: '车辆型号', default: '' },
                { key: 'license_number', value: '车牌号', default: '' },
                { key: 'driver_name', value: '驾驶员', default: '' },
                { key: 'driver_gender', value: '性别', default: '' },
                { key: 'driver_age', value: '年龄', default: '' },
                { key: 'phone', value: '电话', default: '' },
                // { key: 'enterprise', value: '所在企业', default: '' },
            ],
            table_data: [],
            pageSize: 10,
            current_page: 1,
            add_dialog_visible: false,
            delete_row: 0,
            row_index: 0,
            // 新增
            add_form: {
                id: 0,
                type: '',
                license_number: '',
                driver_name: '',
                enterprise_name: ''
            },
            rules: null,
            is_add: false,
            total: 0,
            loading: true
        }
    },

    mounted() {
        this.SizeChange(this.pageSize);
        this.currentPage(this.current_page);
        // this.rules = rules
    },

    created() {
        this.rules = rules
        this.get_total_number()

    },

    methods: {

        // 根据子组件传来的信息，显示/关闭对话框
        is_add_dialog_visible(val) {
            this.add_dialog_visible = val
        },

        async get_total_number() {
            try {
                const res = await this.$http.get("/findRescueVehicleTotalNumber");
                if (res.data.code === 2000) {
                }
                this.total = res.data.data
            } catch (error) {
                // console.log(error);
            }
        },

        async get_cars() {
            this.loading = true
            try {
                const res = await this.$http.post('/findAllRescueVehicle', {
                    index: this.current_page,
                    pagesize: this.pageSize
                });
                // console.log(res)
                this.table_data = res.data.data
                this.get_total_number()
                this.loading = false
            } catch (error) { }
        },

        // 获取子组件传来的数据，当分页数据变化时，发送请求
        async SizeChange(size) {
            this.pageSize = size;
            this.get_cars()
        },

        async currentPage(page) {
            this.current_page = page;
            this.get_cars()
        },

        // 获取子组件传来的row_id
        row_id(val) {
            this.row_index = val
            this.add_form = this.table_data.find(item => item.id === val)
        },
        add(val) {
            this.is_add = val
        },

        async submit() {
            if (this.is_add)
                try {
                    const res = await this.$http.post('/addRescueVehicle', {
                        // 携带数据
                        id: this.row_index,
                        type: this.add_form.type,
                        license_number: this.add_form.license_number,
                        driver_name: this.add_form.driver_name,
                        affiliated_enterprise: this.add_form.enterprise_name,
                        driver_gender: '男',
                        driver_age: 0,
                        phone: '180xxxxxxxx',
                    });
                    // 成功后关闭窗口
                    if (res.data.code === 2000) {
                        this.$message.error('添加成功！');
                        this.$router.go(0)
                    } else if (res.data.code === 4000) {
                        this.$message.error('添加失败！');
                    } else {
                        this.$message.error('出现错误啦！')
                    }
                    this.add_dialog_visible = false
                } catch (error) { }
            else
                try {
                    const res = await this.$http.put('/updateRescueVehicle', {
                        // 携带数据
                        id: this.row_index,
                        type: this.add_form.type,
                        license_number: this.add_form.license_number,
                        driver_name: this.add_form.driver_name,
                        affiliated_enterprise: this.add_form.enterprise_name,
                        driver_gender: '男',
                        driver_age: 0,
                        phone: '180xxxxxxxx',
                    });
                    // 成功后关闭窗口
                    if (res.data.code === 2000) {
                        console.log(res.data.data)
                        this.$router.go(0)
                    } else if (res.data.code === 4000) {
                        this.$message.error('修改失败！');
                    } else {
                        this.$message.error('出现错误啦！')
                    }
                    this.add_dialog_visible = false
                } catch (error) { }
        },
    }
}
</script>

<style lang="less" scoped>
.table_box {
    width: 100%;
    height: 100%;
}

.el-input {
    display: inline-block;
}
</style>